#pragma once

#include <iostream>
#include <cstring>
#include <string>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

class Sock{
private:
public:
  Sock(){}
  static int Socket()
  {
    int sockfd = sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0){
      std::cerr << "socket error" << std::endl;
      exit(1);
    }
    std::cout << sockfd << std::endl;

    return sockfd;
  }

  static void Bind(int listen_sockfd, uint16_t port)
  {
    struct sockaddr_in local;
    memset(&local, 0, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_port = port;
    local.sin_addr.s_addr = INADDR_ANY;
    socklen_t len = sizeof(local);
    if(bind(listen_sockfd, (struct sockaddr*)&local, len) == -1){
      std::cerr << "bind error" << std::endl;
      exit(3);
    }

    std::cout << "bind success" << std::endl;
  }

  static void Listen(int listen_sockfd)
  {
    static const int backlog = 5;
    if(listen(listen_sockfd, backlog) == -1){
      std::cerr << "listen error" << std::endl;
      exit(4);
    }

    std::cout << "listen success" << std::endl;
  }

  static void Accept(int listen_sockfd, int& sockfd)
  {
    struct sockaddr_in peer;
    socklen_t len = sizeof(peer);
    if(sockfd = accept(listen_sockfd, (struct sockaddr*)&peer, &len) == -1){
      std::cerr << "acceot error" << std::endl;
      exit(5);
    }

    std::cout << "get a new link" << std::endl;
  }

  static void Connect(int sockfd)
  {
    struct sockaddr_in peer;
    if(connect(sockfd, (struct sockaddr*)&peer, sizeof(peer)) == -1){
      std::cerr << "connect error" << std::endl;
      exit(6);
    }
  }

  ~Sock(){}
};
